
import org.scalatest._ 
import flatspec._ 
import matchers._ 
import scala.util.Random
import comlib._

class MapSpec extends AnyFlatSpec {

  behavior of "map"

  it should "support simple map function" in {
    val lst = List(1,2,3,4)
    val ret = map( (x:Int)=>x+1, lst.iterator ).toList
    assert( ret == List(2,3,4,5) )
  }

}

class RangeSpec extends AnyFlatSpec{
  behavior of "range"

  it should "support default unlimmitted increase from 0" in {
    val r = range()
    for( i <- 0 until 100 ){
      val nxt = r.next()
      assert( i == nxt, f"i is: ${i} and next is: ${nxt}" )
    }
  }

  it should "support unlimmitted increase from begin" in {
    val r = range(-100)
    for( i <- 0 to 100 ){
      val nxt = r.next()
      assert( i-100 == nxt )
    }    
  }

  it should "支持有限递增" in {
    val ret = range(1, 19, 2).toList
    assert( ret == List(1,3,5,7,9,11,13,15,17) )
  }
}

class CrossSpec extends AnyFlatSpec {
  behavior of "cross"

  it should "支持从向量中选择2个元素的组合" in {
    val ite = cross( List(1,2,3,4).iterator )
    val result = Seq(
      List(1,2), List(1,3), List(1,4),
      List(2,3), List(2,4),
      List(3,4)
    )
    for( r <- result ){
      assert( ite.hasNext == true )
      assert( r == ite.next() )
    }
    assert( ite.hasNext == false )
  }

  it should "支持2向量叉积" in {
    val ite = cross( List(5,6).iterator, List(7,8).iterator )
    val result = Seq( (5,7), (5,8), (6,7), (6,8) )
    for( r <- result ){
      assert( ite.hasNext == true )
      assert( r == ite.next() )
    } 
    assert( ite.hasNext == false )
  }

  it should "支持3向量叉积" in {
    val ite = cross( List(1,2).iterator, List(3,4).iterator, List(5,6).iterator )
    val result = Seq( (1,3,5), (1,3,6), (1,4,5), (1,4,6), (2,3,5), (2,3,6), (2,4,5), (2,4,6) )
    for( r <- result ){
      assert( ite.hasNext == true )
      assert( r == ite.next() )
    }
    assert( ite.hasNext == false )
  }

  it should "支持4向量叉积" in {
    val ite = cross( List(1,2).iterator, List(3,4).iterator, List(5,6).iterator, List(7,8).iterator )
    val result = Seq( 
      (1,3,5,7), (1,3,5,8), (1,3,6,7), (1,3,6,8), 
      (1,4,5,7), (1,4,5,8), (1,4,6,7), (1,4,6,8), 
      (2,3,5,7), (2,3,5,8), (2,3,6,7), (2,3,6,8), 
      (2,4,5,7), (2,4,5,8), (2,4,6,7), (2,4,6,8) )
    for( r <- result ) assert( r == ite.next(), f"The position sequence is: ${result.indexOf(r)}" )
    assert( ite.hasNext == false )
  }
}